package cn.edu.scau.cmi.wangmaoren.client.classicsDesignPattern;

import af.javafx.layout.AfLinearLayout;
import cn.edu.scau.cmi.wangmaoren.abstractFactory.domainAbstractClass.Meat;
import cn.edu.scau.cmi.wangmaoren.abstractFactory.factory.AbstractFactory;
import cn.edu.scau.cmi.wangmaoren.abstractFactory.factory.AonongFactory;
import cn.edu.scau.cmi.wangmaoren.abstractFactory.factory.TianbangFactory;
import cn.edu.scau.cmi.wangmaoren.abstractFactory.factory.WensFactory;
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class AbstractFactoryClient extends Application
{
	private AfLinearLayout root = new AfLinearLayout();
	private AfLinearLayout line = new AfLinearLayout();

	private Button produceMeatBotton = new Button("生产");

	private Label companyLabel = new Label("企业");
	private Label MeatLabel = new Label("肉类");

	private ComboBox<String> companyComboBox = new ComboBox<>();
	private ComboBox<String> meatComboBox = new ComboBox<>();

	private Label leftBlank = new Label();
	private Label rightBlank = new Label();

	private TextArea inforText = new TextArea();

	@Override
	public void start(Stage primaryStage)
	{
		try
		{
			initLayout();// 初始化界面

			Scene scene = new Scene(root, 750, 463);
			primaryStage.setTitle("抽象工厂模式");
			primaryStage.setScene(scene);
			primaryStage.show();

		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public static void main(String[] args)
	{
		launch(args);
	}
	
	public void openWindow()
	{
		initLayout();//初始化界面
		
		Scene scene = new Scene(root,750,463);
		Stage stage = new Stage();
		stage.setTitle("抽象工厂模式");
		stage.setScene(scene);
		stage.show();
	}

	/*
	 * 生产鸡肉
	 */
	private void produceMeat()
	{
		AbstractFactory factory = null;
		Meat meat = null;

		String companyName = companyComboBox.getSelectionModel().getSelectedItem().toString().trim();
		String meatKind = meatComboBox.getSelectionModel().getSelectedItem().toString().trim();

		if (companyName.equals("温氏集团"))
		{
			factory = new WensFactory();
		} else if (companyName.equals("天邦股份"))
		{
			factory = new TianbangFactory();
		} else if (companyName.equals("傲农生物"))
		{
			factory = new AonongFactory();
		}

		if (meatKind.equals("猪肉"))
		{
			meat = factory.createPork();
			inforText.appendText("\n" + meat.cook() + "\n");
		} else if (meatKind.equals("牛肉"))
		{
			meat = factory.createBeef();
			inforText.appendText("\n" + meat.cook() + "\n");
		} else if (meatKind.equals("鸡肉"))
		{
			meat = factory.createChicken();
			inforText.appendText("\n" + meat.cook() + "\n");
		}
	}

	/*
	 * 初始化界面
	 */
	private void initLayout()
	{
		root.orientation(Orientation.VERTICAL).spacing(20).padding(20);
		line.spacing(38);

		line.getChildren().addAll(leftBlank, companyLabel, companyComboBox, MeatLabel, meatComboBox, produceMeatBotton,
				rightBlank);
		line.layout("1 40px 120px 40px 100px 100px 1");
		root.add(line, "50px");
		root.add(inforText, "1");

		companyComboBox.getItems().add("温氏集团");
		companyComboBox.getItems().add("天邦股份");
		companyComboBox.getItems().add("傲农生物");
		companyComboBox.getSelectionModel().select(0);

		meatComboBox.getItems().add("猪肉");
		meatComboBox.getItems().add("牛肉");
		meatComboBox.getItems().add("鸡肉");
		meatComboBox.getSelectionModel().select(0);

		companyLabel.setFont(Font.font("华文中宋", 20));
		MeatLabel.setFont(Font.font("华文中宋", 20));
		meatComboBox.setMaxWidth(30);
		produceMeatBotton.setFont(Font.font("华文中宋", 20));
		inforText.setFont(Font.font("华文中宋", 18));

		Image produceImage = new Image(
				getClass().getResourceAsStream("/cn/edu/scau/cmi/wangmaoren/simpleFactoryMethod/produce.png"));
		produceMeatBotton.setGraphic(new ImageView(produceImage));

		produceMeatBotton.setOnAction((e) -> {
			produceMeat();
		});
	}
}

/*
 * public class AbstractFactoryClient { public static void main(String[] args) {
 * // (1) 选择品牌 System.out.
 * println("Please select which brand you want to buy: 傲农生物, 天邦股份 or 温氏集团???");
 * Scanner scanner = new Scanner(System.in); String brandKind = scanner.next();
 * AbstractFactory factory = null; switch (brandKind) { case "傲农生物": factory =
 * new AonongFactory(); break; case "天邦股份": factory = new TianbangFactory();
 * break; case "温氏集团": factory = new WensFactory(); break; default:
 * System.out.println("sorry, your input is not correct");
 * 
 * }
 * 
 * // （2）选择产品 System.out.
 * println("Please select which product you want to buy: beef, chicken or pork???"
 * ); String productType = scanner.next(); Meat meat = null; switch
 * (productType) { case "beef": meat = factory.createBeef(); break; case
 * "chicken": meat = factory.createChicken(); break; case "pork": meat =
 * factory.createPork(); break; default:
 * System.out.println("sorry, your input is not correct"); } if (meat != null) {
 * meat.cook(); } } }
 */
